<?php


namespace app\award;

use app\models\user\User;
use app\models\user\UserLevel;

/**
 *
 * Chain 新操作
 */
class C
{

    // 获取推荐人
    public static function getReferrer($user_id)
    {
        // 推荐人ID
        $referrer_id = User::find()
            ->where(['id' => $user_id])
            ->select(['referrer_id'])
            ->scalar();

        // 获取推荐人
        if ($referrer_id > 0) {
            $user =  User::find()
                ->where(['id' => $referrer_id])
                ->select([
                    'id', 'level',
                    'v1', 'v2', 'v3', 'v4', 'v5',
                    'c1', 'c2',
                    'c3', 'c4'
                ])
                ->one();
            return $user;
        }

        return null;
    }

    // 获取推荐人链接
    // 都是有等级的
    public static function getReferrerChain($user_id, array $list = [])
    {
        $user = self::getReferrer($user_id);

        if ($user) {
            array_push($list, $user);
            return self::getReferrerChain($user['id'], $list);
        }

        return $list;
    }

    // 获取最大的排位
    public static function getChildrenMax($referrer_id)
    {
        $list = User::find()
            ->where(['referrer_id' => $referrer_id])
            ->andWhere(['!=', 'sort', 9999])
            ->select(['sort'])
            ->asArray()
            ->all();

        if ($list) {
            $max = $list[0]['sort'];
            foreach ($list as $item) {
                if ($item['sort'] == 0 || $item['sort'] >= 9999) {
                    continue;
                }
                if ($item['sort'] > $max) {
                    $max = $item['sort'];
                }
            }
            return intval($max);
        }

        return 0;
    }

    // 直推
    public static function getChildren($referrer_id, $v = '', $high = false)
    {
        $query = User::find()
            ->where(['referrer_id' => $referrer_id])
            // 这个条件加上会中断
            // ->andWhere(['>=', 'level', UserLevel::$keys['vip1']])
            ->select([
                'id', 'level',
                'v1', 'v2', 'v3', 'v4', 'v5',
                'c1', 'c2',
                'c3', 'c4'
            ]);
        if ($v == 'v1') {
            if ($high) {
                $query->andWhere(['v1' => UserLevel::$keys['vip2']]);
            } else {
                $query->andWhere(['>=', 'v1', UserLevel::$keys['vip1']]);
            }
        }
        if ($v == 'v2') {
            if ($high) {
                $query->andWhere(['v2' => UserLevel::$keys['vip4']]);
            } else {
                $query->andWhere(['>=', 'v2', UserLevel::$keys['vip3']]);
            }
        }
        if ($v == 'v3') {
            if ($high) {
                $query->andWhere(['v3' => UserLevel::$keys['vip6']]);
            } else {
                $query->andWhere(['>=', 'v3', UserLevel::$keys['vip5']]);
            }
        }
        if ($v == 'v4') {
            if ($high) {
                $query->andWhere(['v4' => UserLevel::$keys['vip8']]);
            } else {
                $query->andWhere(['>=', 'v4', UserLevel::$keys['vip7']]);
            }
        }
        if ($v == 'v5') {
            if ($high) {
                $query->andWhere(['v5' => UserLevel::$keys['vip10']]);
            } else {
                $query->andWhere(['>=', 'v5', UserLevel::$keys['vip9']]);
            }
        }
        return $query->all();
    }

    // 团队人数
    public static function getChildrenAll($user_id, $v = '', $high = false, array $list = [])
    {
        $children = self::getChildren($user_id, $v);

        if (!empty($children)) {
            foreach ($children as $user) {
                array_push($list, $user);
                $list = self::getChildrenAll($user['id'], $v, $high, $list);
            }
        }

        return $list;
    }

    // 2+1上级
    public static function getReferrerLevel($user_id, $v)
    {
        $referrer_id = User::find()
            ->where(['id' => $user_id])
            ->select(['referrer_id'])
            ->scalar();
        if (!$referrer_id) {
            return null;
        }

        $query = User::find()
            ->where(['id' => $referrer_id])
            ->select([
                'id', 'level',
                'v1', 'v2', 'v3', 'v4', 'v5',
                'c1', 'c2',
                'c3', 'c4'
            ]);
        if ($v == 'v1') {
            $query->andWhere(['v1' => UserLevel::$keys['vip2']]);
        }
        if ($v == 'v2') {
            $query->andWhere(['v2' => UserLevel::$keys['vip4']]);
        }
        if ($v == 'v3') {
            $query->andWhere(['v3' => UserLevel::$keys['vip6']]);
        }
        if ($v == 'v4') {
            $query->andWhere(['v4' => UserLevel::$keys['vip8']]);
        }
        if ($v == 'v5') {
            $query->andWhere(['v5' => UserLevel::$keys['vip10']]);
        }

        return $query->one();
    }

    // 2+1 排线
    public static function getChildrenSort($referrer_id, $v)
    {
        $query = User::find()
            ->where(['referrer_id' => $referrer_id])
            ->andWhere(['>=', 'level', UserLevel::$keys['vip1']])
            ->select([
                'id'
                // 'id', 'level',
                // 'v1', 'v2', 'v3', 'v4', 'v5',
                // 'c1', 'c2',
                // 'c3', 'c4'
            ])
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_ASC]);
        if ($v == 'v1') {
            $query->andWhere(['>=', 'v1', UserLevel::$keys['vip1']]);
        }
        if ($v == 'v2') {
            $query->andWhere(['>=', 'v2', UserLevel::$keys['vip3']]);
        }
        if ($v == 'v3') {
            $query->andWhere(['>=', 'v3', UserLevel::$keys['vip5']]);
        }
        if ($v == 'v4') {
            $query->andWhere(['>=', 'v4', UserLevel::$keys['vip7']]);
        }
        if ($v == 'v5') {
            $query->andWhere(['>=', 'v5', UserLevel::$keys['vip9']]);
        }

        return $query->all();
    }

    // 带等级的链
    public static function getReferrerChainLevel($user_id, $v, array $list = [])
    {
        $user = self::getReferrerLevel($user_id, $v);

        if ($user && $user->level >= UserLevel::$keys['vip1']) {
            array_push($list, $user);
            return self::getReferrerChain($user['id'], $list);
        }

        return $list;
    }

}